<template>
	<div class="node-wrap">
		<div class="node-wrap-box start-node" @click="show">
			<div class="node-wrap-box-title" :style="{background: nodeConfig.color}">
				<i class="icon el-icon-user-solid"></i>
				<span>{{ nodeConfig.nodeName }}</span>
			</div>
			<div class="content">
        <span v-if="nodeConfig.nodeDescription">{{nodeConfig.nodeDescription}}</span>
				<span v-else>{{ toText(nodeConfig) }}</span>
        <img v-show="showCompleteIcon && nodeConfig.complete" class="node-pass-icon" src="../../../assets/pass.png" alt="">
			</div>
		</div>
		<add-node :parentNodeId="nodeConfig.nodeId" :base-url="baseUrl" :headers="headers" v-model="nodeConfig.childNode" :disabled="disabled"></add-node>
		<el-drawer title="发起人" :visible.sync="drawer" destroy-on-close append-to-body :size="500">
			<template #title>
				<div class="node-wrap-drawer__title">
					<label @click="editTitle" v-if="!isEditTitle">{{form.nodeName}}<i class="node-wrap-drawer__title-edit el-icon-edit"></i></label>
					<el-input v-if="isEditTitle" ref="nodeTitle" v-model="form.nodeName" clearable @blur="saveTitle" @keyup.enter="saveTitle"></el-input>
				</div>
			</template>
			<el-container>
				<el-main style="padding:0 20px 20px 20px">
					<el-form label-position="top">
						<!-- <el-form-item label="谁可以发起此审批">
							<el-button type="primary" icon="el-icon-plus" round @click="selectHandle(2, form.nodeRoleList)">选择角色</el-button>
							<div class="tags-list">
								<el-tag v-for="(role, index) in form.nodeRoleList" :key="role.id" type="info" closable @close="delRole(index)">{{role.name}}</el-tag>
							</div>
						</el-form-item> -->
            <el-form-item label="节点描述">
            	<el-input v-model="form.nodeDescription" placeholder="请输入节点描述"></el-input>
            </el-form-item>
            <el-form-item label="卡片顶部颜色">
            	<el-color-picker v-model="form.color"></el-color-picker>
            </el-form-item>
            <el-form-item label="发起人员类型">
              <!-- @change="onChange" -->
            	<el-select v-model="form.setType">
            		<el-option :value="1" label="指定成员"></el-option>
            		<el-option :value="3" label="角色"></el-option>
                <el-option :value="0" label="无需指定"></el-option>
            	</el-select>
            </el-form-item>
            <el-form-item v-if="form.setType==1" label="选择成员">
            	<el-button type="primary" icon="el-icon-plus" round @click="selectHandle(1, form.nodeUserList)">选择人员</el-button>
            	<div class="tags-list">
            		<el-tag v-for="(user, index) in form.nodeUserList" :key="user.id" closable @close="delUser(index)">{{user.name}}</el-tag>
            	</div>
            </el-form-item>
            <el-form-item v-if="form.setType==3" label="选择角色">
            	<el-button type="primary" icon="el-icon-plus" round @click="selectHandle(2, form.nodeRoleList)">选择角色</el-button>
            	<div class="tags-list">
            		<el-tag v-for="(role, index) in form.nodeRoleList" :key="role.id" type="info" closable @close="delRole(index)">{{role.name}}</el-tag>
            	</div>
            </el-form-item>

						<el-alert v-if="form.nodeRoleList && form.nodeRoleList.length==0" title="不指定则默认所有人都可发起此审批" type="info" :closable="false"/>
					</el-form>
				</el-main>
				<el-footer>
					<el-button type="primary" @click="save">保存</el-button>
					<el-button @click="cancel">取消</el-button>
				</el-footer>
			</el-container>
		</el-drawer>
	</div>
</template>

<script>
	import addNode from './addNode.vue'

	export default {
		inject: ['select'],
		props: {
			value: { type: Object, default: () => {} },
      disabled: {
        type: Boolean,
        default: false
      },
      baseUrl: {
        type: String,
        default: ''
      },
      headers: {
        type: Object,
        default: () => {
          return {}
        }
      },
      showCompleteIcon: {
        type: Boolean,
        default: false
      }
		},
		components: {
			addNode
		},
		data() {
			return {
				nodeConfig: {},
				drawer: false,
				isEditTitle: false,
				form: {}
			}
		},
		watch:{
			value(val){
				this.nodeConfig = val
			}
		},
		mounted() {
			this.nodeConfig = this.value
		},
		methods: {
			show(){
        if (this.disabled) return
				this.form = {}
				this.form = JSON.parse(JSON.stringify(this.nodeConfig))
				this.isEditTitle = false
				this.drawer = true
			},
			editTitle(){
				this.isEditTitle = true
				this.$nextTick(()=>{
					this.$refs.nodeTitle.focus()
				})
			},
			saveTitle(){
				this.isEditTitle = false
			},
			selectHandle(type, data){
				this.select(type, data)
			},
      delUser(index){
      	this.form.nodeUserList.splice(index, 1)
      },
			delRole(index){
				this.form.nodeRoleList.splice(index, 1)
			},
			save(){
        if (this.form.setType === 1) {
          this.form.nodeRoleList = []
        } else if (this.form.setType === 3) {
          this.form.nodeUserList = []
        } else if (this.form.setType === 0) {
          this.form.nodeUserList = []
          this.form.nodeRoleList = []
        }
				this.$emit("input", this.form)
				this.drawer = false
			},
      cancel () {
        this.drawer = false
      },
			toText(nodeConfig){
				if(nodeConfig.nodeRoleList && nodeConfig.nodeRoleList.length > 0){
					return nodeConfig.nodeRoleList.map(item=>item.name).join("、")
				}else{
					return "所有人"
				}
			}
		}
	}
</script>
